<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="完整编号" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入完整编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商圈楼栋" prop="options">
        <el-cascader
          v-model="queryParams.options"
          placeholder="请选择商圈楼栋"
          :options="options"
          @change="handleChange"
          @keyup.enter.native="handleQuery"
        ></el-cascader>
      </el-form-item>
      <el-form-item label="楼层" prop="floor">
        <el-input
          v-model="queryParams.floor"
          placeholder="请输入楼层"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.asset_house_state"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['asset:house:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['asset:house:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['asset:house:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['asset:house:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="houseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="房号" align="center" prop="code" />
      <el-table-column label="完整编号" align="center" prop="name" />
      <!--      <el-table-column label="商圈编号" align="center" prop="blockId" />-->
      <el-table-column label="商圈名称" align="center" prop="blockName" />
      <!--      <el-table-column label="楼栋编号" align="center" prop="buildingId" />-->
      <el-table-column label="楼栋名称" align="center" prop="buildingName" />
      <el-table-column label="楼层" align="center" prop="floor" />
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.asset_house_state" :value="scope.row.state"/>
        </template>
      </el-table-column>
      <el-table-column label="状态结束时间" align="center" prop="stateEndTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.stateEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前拥有单位的业主ID" align="center" prop="ownerId" />
      <el-table-column label="绑定的单位" align="center" prop="stateCompany" />
      <el-table-column label="占地面积" align="center" prop="buildingSquare" />
      <el-table-column label="使用面积" align="center" prop="useSquare" />
      <el-table-column label="租金" align="center" prop="rentFee" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['asset:house:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['asset:house:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商铺管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="房号" prop="code">
          <el-input v-model="form.code" placeholder="请输入房号" />
        </el-form-item>
        <!--        <el-form-item label="完整编号" prop="name">-->
        <!--          <el-input v-model="form.name" placeholder="请输入完整编号" />-->
        <!--        </el-form-item>-->
        <el-form-item label="商圈楼栋" prop="blockId">
          <el-cascader
            style="width: 100%"
            v-model="form.options"
            placeholder="请选择商圈楼栋"
            :options="options"
            @change="handleChange"
            @keyup.enter.native="handleQuery"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="楼层" prop="floor">
          <el-input v-model="form.floor" placeholder="请输入楼层" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select  style="width: 100%" v-model="form.state" disabled placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.asset_house_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="占地面积" prop="buildingSquare">
          <el-input v-model="form.buildingSquare" placeholder="请输入占地面积" >
            <template slot="append">㎡</template>
          </el-input>
        </el-form-item>
        <el-form-item label="使用面积" prop="useSquare">
          <el-input v-model="form.useSquare" placeholder="请输入使用面积" >
            <template slot="append">㎡</template>
          </el-input>
        </el-form-item>
        <el-form-item label="租金" prop="rentFee">
          <el-input v-model="form.rentFee" placeholder="请输入租金" >
            <template slot="append">元/月</template>
          </el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHouse, getHouse, delHouse, addHouse, updateHouse } from "@/api/system/house";
import {getAllBuildingCascade} from "@/api/system/building";

export default {
  name: "House",
  dicts: ['asset_house_state'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商铺管理表格数据
      houseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        blockId: null,
        buildingId: null,
        floor: null,
        state: null,
        options:[]
      },
      // 表单参数
      form: {
        options:[]
      },
      // 表单校验
      rules: {
        code: [
          { required: true, message: "房号不能为空", trigger: "blur" }
        ],
        // blockId: [
        //   { required: true, message: "商圈编号不能为空", trigger: "blur" }
        // ],
        // buildingId: [
        //   { required: true, message: "楼栋编号不能为空", trigger: "blur" }
        // ],
        floor: [
          { required: true, message: "楼层不能为空", trigger: "blur" }
        ],
        buildingSquare: [
          { required: true, message: "占地面积不能为空", trigger: "blur" }
        ],
        useSquare: [
          { required: true, message: "使用面积不能为空", trigger: "blur" }
        ],
      },
      options:[]
    };
  },
  created() {
    this.getList();
    //查询商圈楼栋的级联数据
    getAllBuildingCascade().then(res=>{
      this.options=res.data;
    })
  },
  methods: {
    /** 查询商铺管理列表 */
    getList() {
      if(this.queryParams.options.length>0){
        this.queryParams.blockId=this.queryParams.options[0];
        this.queryParams.buildingId=this.queryParams.options[1];
      }
      this.loading = true;
      listHouse(this.queryParams).then(response => {
        this.houseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        blockId: null,
        buildingId: null,
        floor: null,
        state: "empty",
        stateEndTime: null,
        ownerId: null,
        stateCompany: null,
        buildingSquare: null,
        useSquare: null,
        rentFee: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.blockId=undefined;
      this.queryParams.buildingId=undefined;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商铺管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getHouse(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商铺管理";
        //处理级联的反选
        this.form.options=[this.form.blockId+"",this.form.buildingId+""]
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.options.length>0){
            this.form.blockId=this.form.options[0];
            this.form.buildingId=this.form.options[1];
          }
          if (this.form.id != null) {
            updateHouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商铺管理编号为"' + ids + '"的数据项？').then(function() {
        return delHouse(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('asset/house/export', {
        ...this.queryParams
      }, `house_${new Date().getTime()}.xlsx`)
    },
    //当级联选择器条件发生变化时触发
    handleChange(value){
      // this.form.blockId=value[0];
      // this.form.buildingId=value[1];
    }
  }
};
</script>
